document.write("<script src=" + getContextPath() + "'/static/js/drag/drag-arrange.js'><\/script>");

/**
 * 图片上传
 * @param url 上传接口地址 默认：/file/uploadFile
 * @param elem 上传元素对象 默认：#uploadImg
 * @param multiple 是否多文件上传 true false
 * @param mutifileImg 多文件上传后创建图片的DIV 默认：'#mutifileImg'
 */
function $uploadImg(options){
	var url = options.url == undefined ?  "/file/uploadFile" : options.url;
	var elem = options.elem == undefined ?  "#uploadImg" : options.elem;
	var multiple = options.multiple == undefined ?  false : options.multiple;
	var mutifileImg = options.mutifileImg == undefined ?  '#mutifileImg' : options.mutifileImg;
	var loading;
	var rentunMsg = "";
	return options.upload.render({
		 elem: elem
		,url: url
		,multiple : multiple
		,before: function(obj){
			loading = layer.open({
				closeBtn :0,
				type: 1,
				title: "文件上传中",
				shade: 0.5,
				anim: 2,
				content: createLoading()
			});
			// layer.load(); //上传loading
		}
		,progress:function(n, elem){//上传进度回调
			options.element.progress('uploadProgressBar', n+'%'); //设置页面进度条
			if(n == 100){
				$('.layui-loading').append("<div style='margin-top: 10px'>文件压缩中...</div>");
			}
		}
		,allDone: function(obj){ //当文件全部被提交后，才触发
			layer.close(loading);
			layer.msg(rentunMsg);
		}
		,done: function(json){
			if(!multiple) {
				layer.close(loading);
			}
			if(json.code == 0){
				rentunMsg = json.msg;
				if(multiple){
					var fileEntryList = json.data.fileEntryList;
					for(var i=0;i<fileEntryList.length;i++){
						createImgList(mutifileImg,fileEntryList[i]);
					}
				}else{
					var fileUrl = json.data.fileEntryList[0].fileUrl;
					createImg(elem,fileUrl);
				}
			}
			if(!multiple) {
				layer.msg(json.msg);
			}
		}
	});
}

function createLoading(){
	return '<div class="layui-loading">' +
		'<div style="margin-bottom: 5px">上传进度：</div>' +
		'<div class="layui-progress layui-progress-big " lay-showpercent="true" lay-filter="uploadProgressBar">' +
		'<div class="layui-progress-bar" lay-percent="0%"></div>' +
		'</div></div>';
}

function deleteFile(fileUrl,obj,isMultifile){
	if(!isMultifile){
		$(obj).parent().find(".fileUrl").val("");
		$(obj).parent().find('img').attr("src",getContextPath() + "/static/images/addImg.png");
		$(obj).remove();
	}else{
		$(obj).parent().remove();
	}
	layer.msg("删除成功");
 }

function createImg(obj,fileUrl){
	$(obj).parent().find(".closeBtu").remove();
	$(obj).attr('src', getSrcByExt(fileUrl));
	$(obj).parent().find(".fileUrl").val(fileUrl);
	$(obj).parent().append('<div class="closeBtu" onclick="deleteFile(\'' + fileUrl + '\',this)"><img src=' + getContextPath() + '"/static/images/xbtn.png"></div>');
}

function initImg(obj){
	var fileUrl= $(obj).parent().find(".fileUrl").val();
	if(fileUrl){
		$(obj).parent().find(".closeBtu").remove();
		$(obj).attr('src',getSrcByExt(fileUrl));
		$(obj).parent().append('<div class="closeBtu" onclick="deleteFile(\'' + fileUrl + '\',this)"><img src=' + getContextPath() + '"/static/images/xbtn.png"></div>');
	}
}

function initVideo(obj){
	var fileUrl= $(obj).parent().find(".fileUrl").val();
	if(fileUrl){
		$(obj).parent().find(".closeBtu").remove();
		$(obj).attr('src', getContextPath() + "/static/images/fileupload/vedio.png");
		$(obj).parent().append('<div class="closeBtu" onclick="deleteFile(\'' + fileUrl + '\',this)"><img src=' + getContextPath() + '"/static/images/xbtn.png"></div>');
	}
}

function clearImg(obj){
	$(obj).parent().find(".fileUrl").val("");
	$(obj).parent().find('img').attr("src",getContextPath() + "/static/images/addImg.png");
	$(obj).parent().find(".closeBtu").remove();
}

function createImgList(obj,fileEntry){
	var html = '<li class="layui-upload-list"  style="width:100px;">';
	html += '<input value="' + fileEntry.fileUrl + '" type="hidden" class="fileUrlInput">';

	html += '<img class="layui-upload-img" src="' + getSrcByExt(fileEntry.fileUrl)+ '">';
	html += '<div class="closeBtu" onclick="deleteFile(\'' + fileEntry.fileUrl + '\',this,true)">';
	html += '<img src=' + getContextPath() + '"/static/images/xbtn.png">';
	html += '</div>';
	html += '</li>';
	$(obj).append(html);
}


function createImgListByFileUrl(obj,fileUrl){
	var html = '<li class="layui-upload-list"  style="width:100px;">';
	html += '<input value="' + fileUrl + '" type="hidden" class="fileUrlInput">';
	html += '<img class="layui-upload-img" src="' + getSrcByExt(fileUrl) + '">';
	html += '<div class="closeBtu" onclick="deleteFile(\'' + fileUrl + '\',this,true)">';
	html += '<img src=' + getContextPath() + '"/static/images/xbtn.png">';
	html += '</div>';
	html += '</li>';
	$(obj).append(html);
}

function createImgListNotModi(obj,fileUrl){
	var html = '<li class="layui-upload-list"  style="width:100px;">';
	html += '<img class="layui-upload-img" src="' + getSrcByExt(fileUrl) + '">';
	html += '</li>';
	$(obj).append(html);
}

function getMutifileUrl(obj){
	var fileUrl = "";
	var fileUrlList = $(obj).find(".fileUrlInput");

	for(var i=0;i<fileUrlList.length;i++){
		fileUrl += $(fileUrlList[i]).val() + ",";
	}
	if(fileUrl.length > 0)
		fileUrl = fileUrl.substring(0,fileUrl.length -1);
	return fileUrl;
}

function addFile(obj,file) {
	createFileListByFileUrl(obj,file.fileUrl,file.realFileName);
}
function createFileListByFileUrl(obj,fileUrl){
	var type=getFileExt(fileUrl);
	var staticIcon="/static/images/file.png";
	var typeValue=0;
	if(type=="doc"||type=="docx"){
		staticIcon="/static/images/doc.png";
		typeValue=1;
	}else if(type=="xsl"||type=="xlsx"){
		staticIcon="/static/images/xsl.png";
		typeValue=2;
	}else if(type=="pdf"){
		staticIcon="/static/images/pdf.png";
		typeValue=3;
	}else if(type=="ppt"||type=="pptx"){
		staticIcon="/static/images/pptx.png";
		typeValue=4;
	}else  if(type=="png"||type=="jpge"||type=="jpg"||type=="gif"){
		staticIcon="/static/images/pic.png";
		typeValue=0;
	}else{
		staticIcon="/static/images/file.png";
		typeValue=0;
	}

	var html = '<li class="layui-upload-list"  style="width:100px;">';
	html += '<input value="' + fileUrl + '" type="hidden" class="fileUrlInput">';
	html += '<img class="layui-upload-img" src="'+staticIcon+'">';
	html += '<div class="layui-file-name">'+fileUrl.split("/")[fileUrl.split("/").length - 1]+'</div>';
	html += '<div class="closeBtu" onclick="deleteFile(\'' + fileUrl + '\',this,true)">';
	html += '<img src=' + getContextPath() + '"/static/images/xbtn.png">';
	html += '</div>';
	html += '</li>';
	$(obj).append(html);
}
function  getFileExt (filePath){
	var startIndex = filePath.lastIndexOf(".");
	if(startIndex != -1)
		return filePath.substring(startIndex+1, filePath.length).toLowerCase();
	else return "";
}


/******
 * 文件管理器
 * @param params.selectType 0:无选择按钮（用于直接查看文件管理页面），1：应用于单文件选择，2：应用于多文件选择
 * @param params.objId
 * @param params.showFileType,显示的文件类型(若不为空只支持单个)，1：图片，2：音视频，3：文档，为空则显示全部
 * @param params.storeMode 音视频存储方式，oss/vod，默认oss，vod即阿里视频点播
 */
function $selectFileManage(selectType, objId, showFileType, storeMode){
	var url = getContextPath() + "/sys/file/manage?selectType=" + selectType + "&objId=" + objId ;
	if(showFileType){
		url+= "&showFileType=" + showFileType;
	}
	if(storeMode){
		url+= "&storeMode=" + storeMode
	}
	layer.open({
		title : "文件管理器",
		type: 2,
		area: ['98%', '98%'],
		anim: 2,
		content: [url, 'yes']
	});
}
// function $selectFileManage(showFileType, selectType, objId, valueId){
// 	var url = getContextPath() + "/sys/file/manage?selectType=" + selectType + "&showFileType=" + showFileType+ "&objId=" + objId + "&valueId=" + valueId ;
// 	layer.open({
// 		title : "文件管理器",
// 		type: 2,
// 		area: ['98%', '98%'],
// 		anim: 2,
// 		content: [url, 'yes']
// 	});
// }


/**
 * 文件管理文件上传
 * @param url 上传接口地址 默认：/file/uploadFile
 * @param elem 上传元素对象 默认：#uploadImg
 * @param multiple 是否多文件上传 true false
 * @param mutifileImg 多文件上传后创建图片的DIV 默认：'#mutifileImg'
 * @param storeMode 音视频存储方式，oss/vod，默认oss，vod即阿里视频点播
 */
function $uploadFileManage(options){
	var url = options.url == undefined ?  "/file/uploadFile" : options.url;
	var elem = options.elem == undefined ?  "#addFile" : options.elem;
	var rentunMsg = "";
	var icon = 2;
	var loading;
	return options.upload.render({
		elem: elem
		,url: url
		,multiple : true
		,accept : options.storeMode=='vod'?'audioVideo':'file' //上传至视频点播的只限音视频
		,data : {
			"storeMode": options.storeMode=='vod'?'vod':'oss',
			"dirId": function () {
				return $("#dirId").val();
			}
		}
		,before: function(obj){
			loading = layer.open({
				closeBtn :0,
				type: 1,
				title: "文件上传中",
				shade: 0.5,
				anim: 2,
				content: createLoading()
			});
			// layer.load(); //上传loading
		}
		,progress:function(n, elem){//上传进度回调
			options.element.progress('uploadProgressBar', n+'%'); //设置页面进度条
			if(n == 100){
				$('.layui-loading').append("<div style='margin-top: 10px'>文件压缩中...</div>");
			}
		}
		,allDone: function(obj){ //当文件全部被提交后，才触发
			layer.close(loading);
			layer.msg(rentunMsg, {icon: icon});
		}
		,done: function(json){
			rentunMsg = json.msg;
			if(json.code == 0){
				icon = 1;
				addFile(json.data.fileEntryList);
			}else{
				icon = 2;
			}
		}
	});
}

var imgExt = [
	["gif,jpg,jpeg,png,bmp"],
	["swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,mp4","/static/images/fileupload/vedio.png"],
	["doc,docx","/static/images/fileupload/word.png"],
	["xls,xlsx","/static/images/fileupload/excel.png"],
	["pdf","/static/images/fileupload/pdf.png"],
	["zip,rar,gz,bz2","/static/images/fileupload/zip.png"],
	["other","/static/images/fileupload/wj.png"]
];

function getSrcByExtV(fileUrl){
	return getImgeUrl(fileUrl,'3');
}

function getSrcByExt(fileUrl){
	var ext = getFileExt(fileUrl); //根据文件url后缀
	return getIconSrcByExt(fileUrl, ext);
}

/**
 * 主要用于融合视频点播方式，其url数据库存储的是视频ID，即时是播放url也没有代表文件类型的后缀
 * @param fileUrl
 * @param ext
 * @returns {string|string}
 */
function getIconSrcByExt(fileUrl, ext){
	var imgsTest = $.inArray(ext,imgExt[0][0].split(","));
	if(imgsTest != -1){
		return getImgeUrl(fileUrl,'3');
	}
	var src = "/static/images/fileupload/wj.png";
	for(var i = 1;i < imgExt.length; i++){
		var exts = imgExt[i][0].split(",");
		var arr = $.inArray(ext,exts);
		if(arr == -1){
			continue;
		}else{
			src = imgExt[i][1];
			break;
		}
	}
	return getContextPath() + src;
}

function imgDrag(){
	$('li').arrangeable({dragSelector: '.layui-upload-img'});
}
setTimeout(imgDrag,100);